package com.purong.purong.activity;

import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.bean.BankLoanRecordBean;
import com.purong.purong.bean.BankLoanRecordDetBean;
import com.purong.purong.bean.SmallLoanRecordBean;
import com.purong.purong.bean.SmallLoanRecordDetBean;
import com.purong.purong.util.CommonUtil;
import com.purong.purong.widget.LoadingView;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;

/**
 * Created by zengwenjie on 2017/12/11.
 */
public class LoanRecordDetActivity extends BaseActivity {
    public static final int BANK_LOAN=0X001,SMALL_LOAN=0X002;
    private RelativeLayout topLay;
    private ImageView topImg,tagOne,tagTwo,tagThree,lineOne,lineTwo;
    private TextView topStatus,titleOne,titleTwo,titleThree,period,name,date,money,note;
    private LinearLayout noteLay;
    private String applyId;
    private LoadingView loadingView;
    private int type;
    @Override
    public int getRootViewId() {
        return R.layout.activity_loan_record_det;
    }

    @Override
    public void initBundle() {
        applyId=getIntent().getStringExtra("APPLY_ID");
        type=getIntent().getIntExtra("TYPE",BANK_LOAN);
    }

    @Override
    public void initView() {
        showTitle("贷款详情");
        showBack();
        loadingView=(LoadingView)findViewById(R.id.loading_view);
        loadingView.setLoadListener(new LoadingView.OnLoadListener() {
            @Override
            public void onReload() {
                getBankLoanDet();
            }

            @Override
            public void onRefresh() {

            }
        });
        topLay=(RelativeLayout)findViewById(R.id.top_lay);
        topLay.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,(int)(CommonUtil.getScreenWidth(this)/3.74)));
        topImg=(ImageView)findViewById(R.id.top_img);
        topStatus=(TextView)findViewById(R.id.top_status);
        tagOne=(ImageView)findViewById(R.id.tag_one);
        tagTwo=(ImageView)findViewById(R.id.tag_two);
        tagThree=(ImageView)findViewById(R.id.tag_three);
        lineOne=(ImageView)findViewById(R.id.line_one);
        lineTwo=(ImageView)findViewById(R.id.line_two);
        titleOne=(TextView)findViewById(R.id.title_one);
        titleTwo=(TextView)findViewById(R.id.title_two);
        titleThree=(TextView)findViewById(R.id.title_three);
        period=(TextView)findViewById(R.id.period);
        name=(TextView)findViewById(R.id.name);
        date=(TextView)findViewById(R.id.date);
        money=(TextView)findViewById(R.id.money);
        note=(TextView)findViewById(R.id.note);
        noteLay=(LinearLayout)findViewById(R.id.note_lay);
    }
    private void setData(BankLoanRecordBean bean){
        if (bean==null)
            return;
        if (bean.getLoan_apply_status().equals("1")){
            topImg.setImageResource(R.drawable.loan_ing);
            topStatus.setText(bean.getLoan_apply_status_name());
        }else if (bean.getLoan_apply_status().equals("2")){
            topImg.setImageResource(R.drawable.loan_ing);
            topStatus.setText(bean.getLoan_apply_status_name());
            if (bean.getLoan_apply_status_step().equals("1")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                titleOne.setTextColor(getResources().getColor(R.color.blue));
            }else if (bean.getLoan_apply_status_step().equals("2")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                titleOne.setTextColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_sel);
                lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
                titleTwo.setTextColor(getResources().getColor(R.color.blue));
            }else if (bean.getLoan_apply_status_step().equals("3")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                titleOne.setTextColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_sel);
                lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
                titleTwo.setTextColor(getResources().getColor(R.color.blue));
                tagThree.setImageResource(R.drawable.profile_iocn_sel);
                titleThree.setTextColor(getResources().getColor(R.color.blue));
            }
        }else if (bean.getLoan_apply_status().equals("3")){
            topImg.setImageResource(R.drawable.loan_fail);
            topStatus.setText(bean.getLoan_apply_status_name());
            if (bean.getLoan_audit_note()!=null&&!bean.getLoan_audit_note().equals("")){
                noteLay.setVisibility(View.VISIBLE);
                note.setText(bean.getLoan_audit_note());
            }
            if (bean.getLoan_apply_status_step().equals("1")){
                tagOne.setImageResource(R.drawable.profile_iocn_error);
                titleOne.setTextColor(getResources().getColor(R.color.red));
            }else if (bean.getLoan_apply_status_step().equals("2")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_error);
                titleTwo.setTextColor(getResources().getColor(R.color.red));
            }else if (bean.getLoan_apply_status_step().equals("3")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_sel);
                lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
                tagThree.setImageResource(R.drawable.profile_iocn_error);
                titleThree.setTextColor(getResources().getColor(R.color.red));
            }
        }else {
            topImg.setImageResource(R.drawable.loan_success);
            topStatus.setText(bean.getLoan_apply_status_name());
            tagOne.setImageResource(R.drawable.profile_iocn_sel);
            tagTwo.setImageResource(R.drawable.profile_iocn_sel);
            tagThree.setImageResource(R.drawable.profile_iocn_sel);
            lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
            lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
            titleOne.setTextColor(getResources().getColor(R.color.blue));
            titleTwo.setTextColor(getResources().getColor(R.color.blue));
            titleThree.setTextColor(getResources().getColor(R.color.blue));
        }
        name.setText(bean.getLoan_product_name());
        date.setText(bean.getLoan_apply_time());
        money.setText(bean.getApply_money()+"万元");
        period.setText(bean.getApply_period()+"个月");
    }
    private void setData(SmallLoanRecordBean bean){
        if (bean==null)
            return;

        if (bean.getSmall_apply_status().equals("1")){
            topImg.setImageResource(R.drawable.loan_ing);
            topStatus.setText(bean.getSmall_apply_status_name());
        }else if (bean.getSmall_apply_status().equals("2")){
            topImg.setImageResource(R.drawable.loan_ing);
            topStatus.setText(bean.getSmall_apply_status_name());
            if (bean.getSmall_apply_status_step().equals("1")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                titleOne.setTextColor(getResources().getColor(R.color.blue));
            }else if (bean.getSmall_apply_status_step().equals("2")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                titleOne.setTextColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_sel);
                lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
                titleTwo.setTextColor(getResources().getColor(R.color.blue));
            }else if (bean.getSmall_apply_status_step().equals("3")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                titleOne.setTextColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_sel);
                lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
                titleTwo.setTextColor(getResources().getColor(R.color.blue));
                tagThree.setImageResource(R.drawable.profile_iocn_sel);
                titleThree.setTextColor(getResources().getColor(R.color.blue));
            }
        }else if (bean.getSmall_apply_status().equals("3")){
            topImg.setImageResource(R.drawable.loan_fail);
            topStatus.setText(bean.getSmall_apply_status_name());
            if (bean.getSmall_audit_note()!=null&&!bean.getSmall_audit_note().equals("")){
                noteLay.setVisibility(View.VISIBLE);
                note.setText(bean.getSmall_audit_note());
            }
            if (bean.getSmall_apply_status_step().equals("1")){
                tagOne.setImageResource(R.drawable.profile_iocn_error);
                titleOne.setTextColor(getResources().getColor(R.color.red));
            }else if (bean.getSmall_apply_status_step().equals("2")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_error);
                titleTwo.setTextColor(getResources().getColor(R.color.red));
            }else if (bean.getSmall_apply_status_step().equals("3")){
                tagOne.setImageResource(R.drawable.profile_iocn_sel);
                lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
                tagTwo.setImageResource(R.drawable.profile_iocn_sel);
                lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
                tagThree.setImageResource(R.drawable.profile_iocn_error);
                titleThree.setTextColor(getResources().getColor(R.color.red));
            }
        }else {
            topImg.setImageResource(R.drawable.loan_success);
            topStatus.setText(bean.getSmall_apply_status_name());
            tagOne.setImageResource(R.drawable.profile_iocn_sel);
            tagTwo.setImageResource(R.drawable.profile_iocn_sel);
            tagThree.setImageResource(R.drawable.profile_iocn_sel);
            lineOne.setBackgroundColor(getResources().getColor(R.color.blue));
            lineTwo.setBackgroundColor(getResources().getColor(R.color.blue));
            titleOne.setTextColor(getResources().getColor(R.color.blue));
            titleTwo.setTextColor(getResources().getColor(R.color.blue));
            titleThree.setTextColor(getResources().getColor(R.color.blue));
        }
        name.setText(bean.getSmall_product_name());
        date.setText(bean.getSmall_apply_time());
        money.setText(bean.getApply_money()+"元");
//        if (Integer.valueOf(bean.getApply_money())<10000){
//            money.setText(bean.getApply_money()+"元");
//        }else {
//            money.setText(CommonUtil.numConvert(Integer.valueOf(bean.getApply_money()),2,"万元"));
//        }
        period.setText(bean.getApply_period()+"个月");
    }
    private void getBankLoanDet(){
        if (applyId==null||applyId.equals("")){
            loadingView.fail();
            return;
        }else {
            loadingView.loading();
        }
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("loan_apply_id",applyId);
        if (!request(Contant.LOAN_APPLY_INFO, params, true)) {
            loadingView.fail();
        }
    }
    private void getSmallLoanDet(){
        if (applyId==null||applyId.equals("")){
            loadingView.fail();
            return;
        }else {
            loadingView.loading();
        }
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("small_apply_id",applyId);
        if (!request(Contant.SMALL_APPLY_INFO, params, true)) {
            loadingView.fail();
        }
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.LOAN_APPLY_INFO)){
            if (responseInfo.isRequestOk){
                try{
                    BankLoanRecordDetBean bean= JSON.parseObject(responseInfo.content,BankLoanRecordDetBean.class);
                    if (bean!=null&&bean.getLoan_apply_info()!=null){
                        setData(bean.getLoan_apply_info());
                    }
                    loadingView.finish();
                }catch (Exception e){
                    loadingView.fail();
                }
            }else {
                loadingView.fail();
            }
        }else if (responseInfo.url.equals(Contant.SMALL_APPLY_INFO)){
            if (responseInfo.isRequestOk){
                try{
                    SmallLoanRecordDetBean bean= JSON.parseObject(responseInfo.content,SmallLoanRecordDetBean.class);
                    if (bean!=null&&bean.getSmall_apply_info()!=null){
                        setData(bean.getSmall_apply_info());
                    }
                    loadingView.finish();
                }catch (Exception e){
                    loadingView.fail();
                }
            }else {
                loadingView.fail();
            }
        }
    }

    @Override
    public void initData() {
        if (type==BANK_LOAN){
            getBankLoanDet();
        }else {
            getSmallLoanDet();
        }

    }
}
